using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace RapidHDL
{
    static class ShellControl
    {
        public static void CheckForProcess(string pProcessName, string pProcessExePath, string pStartupFolder)
        {
            int count = GetProcessCount(pProcessName);
            if (count > 1)
            {
                KillAllProcesses(pProcessName);
                count = GetProcessCount(pProcessName);
                System.Threading.Thread.Sleep(10000);
                count = GetProcessCount(pProcessName);
            }
            if (count == 0)
            {
                StartExe(pProcessExePath, pStartupFolder);
            }
        }

        public static int GetProcessCount(string pProcessName)
        {
            Process[] processes = Process.GetProcesses();
            int num = 0;
            Process[] processArray2 = processes;
            int length = processArray2.Length;
            for (int i = 0; i < length; i++)
            {
                Process oProcess = processArray2[i];
                if (ProcessName(oProcess).Trim().ToUpper() == pProcessName.Trim().ToUpper())
                {
                    num++;
                }
                //Console.WriteLine(ProcessName(oProcess).Trim().ToUpper());
            }
            Console.WriteLine("Count " + pProcessName + " " + num.ToString());
            return num;

         /*   Process[] processList = Process.GetProcesses();

            int count = 0;
            foreach (Process process in processList)
            {
                if (process.ProcessName.Trim().ToUpper() == pProcessName.Trim().ToUpper())
                    count++;
                //Console.WriteLine(process.ProcessName.Trim().ToUpper());
            }
            return count;*/
        }

        public static int KillAllProcesses(string pProcessName)
        {
            Process[] processList = Process.GetProcesses();
            Console.WriteLine("killing processes " + pProcessName);

            int count = 0;
            foreach (Process process in processList)
            {
                if (process.ProcessName.Trim().ToUpper() == pProcessName.Trim().ToUpper())
                {
                    Console.WriteLine("Killing " + process.ProcessName);
                    process.Kill();
                }
            }
            return count;
        }

        public static void StartExe(string pProcessExePath, string pStartupFolder)
        {
            Process newExe = new Process();

            if (!Utility.IsUnix())
            {
                if (!pProcessExePath.Trim().ToUpper().EndsWith(".EXE") && !pProcessExePath.Trim().ToUpper().EndsWith(".COM"))
                    newExe.StartInfo.FileName = pProcessExePath + ".EXE";
            }
            else
            {
                newExe.StartInfo.FileName = pProcessExePath;
            }

            
            newExe.StartInfo.WorkingDirectory = pStartupFolder;
            newExe.Start();
        }

        public static string ProcessName(Process oProcess)
        {
            try
            {
                return oProcess.ProcessName;
            }
            catch
            {
                return ("ERROR " + oProcess.StartInfo.FileName);
            }
        }

        public static string StartExe(string pProcessExePath, string pStartupFolder, string lineSwitch)
        {
            return StartExe(pProcessExePath, pStartupFolder, lineSwitch, false);
        }


        public static string StartExe(string pProcessExePath, string pStartupFolder, string lineSwitch, bool pbRedirectOutput)
        {
            Process newExe = new Process();

            if (!Utility.IsUnix())
            {
                if (!pProcessExePath.Trim().ToUpper().EndsWith(".EXE") && !pProcessExePath.Trim().ToUpper().EndsWith(".COM"))
                    newExe.StartInfo.FileName = pProcessExePath + ".EXE";
            }
            else
            {
                newExe.StartInfo.FileName = pProcessExePath;
            }

            if (pbRedirectOutput)
            {
                newExe.StartInfo.RedirectStandardError = true;
                newExe.StartInfo.RedirectStandardOutput = true;
                newExe.StartInfo.UseShellExecute = false;
            }

            if (Utility.IsUnix())
            {
                newExe.StartInfo.WorkingDirectory = pStartupFolder;
                newExe.StartInfo.Arguments = lineSwitch;
                newExe.Start();
                newExe.WaitForInputIdle();
                newExe.WaitForExit();
            }
            else
            {
                newExe.StartInfo.WorkingDirectory = pStartupFolder; 
                newExe.StartInfo.Arguments = lineSwitch; 
                newExe.Start(); 
            }

            if (pbRedirectOutput)
            {
                return newExe.StandardOutput.ReadToEnd() + newExe.StandardError.ReadToEnd();
            }
            else
            {
                return "";
            }
        }

        /*
        public static void StartExe(string pProcessExePath, string pStartupFolder)
        {
            Process newExe = new Process();
            newExe.StartInfo.FileName = pProcessExePath;
            newExe.StartInfo.WorkingDirectory = pStartupFolder;
            newExe.Start();
        }

        public static void StartExe(string pProcessExePath, string pStartupFolder, string lineSwitch)
        {
            Process newExe = new Process();

            if (!Utility.IsUnix())
            {
                if (!pProcessExePath.Trim().ToUpper().EndsWith(".EXE") && !pProcessExePath.Trim().ToUpper().EndsWith(".COM"))
                    newExe.StartInfo.FileName = pProcessExePath + ".EXE";
            }
            else
            {
                newExe.StartInfo.FileName = pProcessExePath;
            }
            
			newExe.StartInfo.WorkingDirectory = pStartupFolder;
            newExe.StartInfo.Arguments = lineSwitch;
            newExe.Start();
            try
            {
                newExe.WaitForInputIdle();
            }
            catch
            {
            }
			newExe.WaitForExit();
        }*/

    }
}
